﻿#pragma once

#ifndef QCEFDELEGATE_H
#define QCEFDELEGATE_H

#include <QtCore>

#include "QCefHeader.h"
#include "QCefDebug.h"


class QCefWidget;

class QCefDelegate : public virtual CefBaseRefCounted
{
public:
    typedef QCefWidget                  OPERATOR_CLASS;
    typedef QAtomicPointer<QCefWidget>  OPERATOR_CLASS_PTR;

public:
    QCefDelegate(QCefWidget * opr = nullptr);
    virtual ~QCefDelegate();

public:
    int finiDelegate();

public:
    QCefWidget * getOperator() { return m_operator.loadAcquire(); }
    void setOperator(QCefWidget * val) { m_operator.storeRelease(val); }

protected:
    QAtomicPointer<QCefWidget> m_operator;

public:
    //////////////////////////////////////////////////////////////////////////
    //CefClient
    virtual bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
                                          CefRefPtr<CefFrame> frame,
                                          CefProcessId source_process,
                                          CefRefPtr<CefProcessMessage> message);


    //////////////////////////////////////////////////////////////////////////
    //CefLifeSpanHandler
    virtual bool OnBeforePopup(CefRefPtr<CefBrowser> browser,
                               CefRefPtr<CefFrame> frame,
                               const CefString& target_url,
                               const CefString& target_frame_name,
                               CefLifeSpanHandler::WindowOpenDisposition target_disposition,
                               bool user_gesture,
                               const CefPopupFeatures& popupFeatures,
                               CefWindowInfo& windowInfo,
                               CefRefPtr<CefClient>& client,
                               CefBrowserSettings& settings,
                               CefRefPtr<CefDictionaryValue>& extra_info,
                               bool* no_javascript_access);

    virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser);

    virtual bool DoClose(CefRefPtr<CefBrowser> browser);

    virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser);


    //////////////////////////////////////////////////////////////////////////
    //CefLoadHandler
    virtual void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
                                      bool isLoading,
                                      bool canGoBack,
                                      bool canGoForward);

    virtual void OnLoadStart(CefRefPtr<CefBrowser> browser,
                             CefRefPtr<CefFrame> frame,
                             CefLoadHandler::TransitionType transition_type);

    virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser,
                           CefRefPtr<CefFrame> frame,
                           int httpStatusCode);

    virtual void OnLoadError(CefRefPtr<CefBrowser> browser,
                             CefRefPtr<CefFrame> frame,
                             CefLoadHandler::ErrorCode errorCode,
                             const CefString& errorText,
                             const CefString& failedUrl);

    //////////////////////////////////////////////////////////////////////////
    //CefDisplayHandler
    virtual void OnAddressChange(CefRefPtr<CefBrowser> browser,
                                 CefRefPtr<CefFrame> frame,
                                 const CefString& url);

    virtual void OnTitleChange(CefRefPtr<CefBrowser> browser,
                               const CefString& title);

    virtual void OnFaviconURLChange(CefRefPtr<CefBrowser> browser,
                                    const std::vector<CefString>& icon_urls);

    virtual void OnFullscreenModeChange(CefRefPtr<CefBrowser> browser,
                                        bool fullscreen);

    virtual bool OnTooltip(CefRefPtr<CefBrowser> browser,
                           CefString& text);

    virtual void OnStatusMessage(CefRefPtr<CefBrowser> browser,
                                 const CefString& value);

    virtual bool OnConsoleMessage(CefRefPtr<CefBrowser> browser,
                                  cef_log_severity_t level,
                                  const CefString& message,
                                  const CefString& source,
                                  int line);

    virtual bool OnAutoResize(CefRefPtr<CefBrowser> browser,
                              const CefSize& new_size);

    virtual void OnLoadingProgressChange(CefRefPtr<CefBrowser> browser,
                                         double progress);

    virtual bool OnCursorChange(CefRefPtr<CefBrowser> browser,
                                CefCursorHandle cursor,
                                cef_cursor_type_t type,
                                const CefCursorInfo& custom_cursor_info);

    //扩展添加的
    virtual void OnFaviconChange(const CefString & image_url,
                                 int http_status_code,
                                 CefRefPtr<CefImage> image);


    //////////////////////////////////////////////////////////////////////////
    //CefFocusHandler
    virtual void OnTakeFocus(CefRefPtr<CefBrowser> browser,
                             bool next);

    virtual bool OnSetFocus(CefRefPtr<CefBrowser> browser,
                            CefFocusHandler::FocusSource source);

    virtual void OnGotFocus(CefRefPtr<CefBrowser> browser);


    //////////////////////////////////////////////////////////////////////////
    //CefRenderHandler
    virtual CefRefPtr<CefAccessibilityHandler> GetAccessibilityHandler();

    virtual bool GetRootScreenRect(CefRefPtr<CefBrowser> browser, CefRect& rect);

    virtual void GetViewRect(CefRefPtr<CefBrowser> browser, CefRect& rect);

    virtual bool GetScreenPoint(CefRefPtr<CefBrowser> browser,
                                int viewX,
                                int viewY,
                                int& screenX,
                                int& screenY);

    virtual bool GetScreenInfo(CefRefPtr<CefBrowser> browser,
                               CefScreenInfo& screen_info);

    virtual void OnPopupShow(CefRefPtr<CefBrowser> browser, bool show);

    virtual void OnPopupSize(CefRefPtr<CefBrowser> browser, const CefRect& rect);

    virtual void OnPaint(CefRefPtr<CefBrowser> browser,
                         CefRenderHandler::PaintElementType type,
                         const CefRenderHandler::RectList& dirtyRects,
                         const void* buffer,
                         int width,
                         int height);

    virtual void OnAcceleratedPaint(CefRefPtr<CefBrowser> browser,
                                    CefRenderHandler::PaintElementType type,
                                    const CefRenderHandler::RectList& dirtyRects,
                                    void* shared_handle);

    virtual bool StartDragging(CefRefPtr<CefBrowser> browser,
                               CefRefPtr<CefDragData> drag_data,
                               CefRenderHandler::DragOperationsMask allowed_ops,
                               int x,
                               int y);

    virtual void UpdateDragCursor(CefRefPtr<CefBrowser> browser,
                                  CefRenderHandler::DragOperation operation);

    virtual void OnScrollOffsetChanged(CefRefPtr<CefBrowser> browser,
                                       double x,
                                       double y);

    virtual void OnImeCompositionRangeChanged(CefRefPtr<CefBrowser> browser,
                                              const CefRange& selected_range,
                                              const CefRenderHandler::RectList& character_bounds);

    virtual void OnTextSelectionChanged(CefRefPtr<CefBrowser> browser,
                                        const CefString& selected_text,
                                        const CefRange& selected_range);

    virtual void OnVirtualKeyboardRequested(CefRefPtr<CefBrowser> browser,
                                            CefRenderHandler::TextInputMode input_mode);


    //////////////////////////////////////////////////////////////////////////
    //CefRequestHandler
    virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
                                CefRefPtr<CefFrame> frame,
                                CefRefPtr<CefRequest> request,
                                bool user_gesture,
                                bool is_redirect);

    virtual bool OnOpenURLFromTab(CefRefPtr<CefBrowser> browser,
                                  CefRefPtr<CefFrame> frame,
                                  const CefString& target_url,
                                  CefRequestHandler::WindowOpenDisposition target_disposition,
                                  bool user_gesture);

    virtual CefRefPtr<CefResourceRequestHandler> GetResourceRequestHandler(CefRefPtr<CefBrowser> browser,
                                                                           CefRefPtr<CefFrame> frame,
                                                                           CefRefPtr<CefRequest> request,
                                                                           bool is_navigation,
                                                                           bool is_download,
                                                                           const CefString& request_initiator,
                                                                           bool& disable_default_handling);

    virtual bool GetAuthCredentials(CefRefPtr<CefBrowser> browser,
                                    const CefString& origin_url,
                                    bool isProxy,
                                    const CefString& host,
                                    int port,
                                    const CefString& realm,
                                    const CefString& scheme,
                                    CefRefPtr<CefAuthCallback> callback);

    virtual bool OnQuotaRequest(CefRefPtr<CefBrowser> browser,
                                const CefString& origin_url,
                                int64 new_size,
                                CefRefPtr<CefCallback> callback);

    virtual bool OnCertificateError(CefRefPtr<CefBrowser> browser,
                                    cef_errorcode_t cert_error,
                                    const CefString& request_url,
                                    CefRefPtr<CefSSLInfo> ssl_info,
                                    CefRefPtr<CefCallback> callback);

    virtual bool OnSelectClientCertificate(CefRefPtr<CefBrowser> browser,
                                           bool isProxy,
                                           const CefString& host,
                                           int port,
                                           const CefRequestHandler::X509CertificateList& certificates,
                                           CefRefPtr<CefSelectClientCertificateCallback> callback);

    virtual void OnRenderViewReady(CefRefPtr<CefBrowser> browser);

    virtual void OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
                                           CefRequestHandler::TerminationStatus status);

    virtual void OnDocumentAvailableInMainFrame(CefRefPtr<CefBrowser> browser);


    //////////////////////////////////////////////////////////////////////////
    //CefResourceRequestHandler
    virtual CefRefPtr<CefCookieAccessFilter> GetCookieAccessFilter(CefRefPtr<CefBrowser> browser,
                                                                   CefRefPtr<CefFrame> frame,
                                                                   CefRefPtr<CefRequest> request);

    virtual CefResourceRequestHandler::ReturnValue OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
                                                                        CefRefPtr<CefFrame> frame,
                                                                        CefRefPtr<CefRequest> request,
                                                                        CefRefPtr<CefCallback> callback);

    virtual CefRefPtr<CefResourceHandler> GetResourceHandler(CefRefPtr<CefBrowser> browser,
                                                             CefRefPtr<CefFrame> frame,
                                                             CefRefPtr<CefRequest> request);

    virtual void OnResourceRedirect(CefRefPtr<CefBrowser> browser,
                                    CefRefPtr<CefFrame> frame,
                                    CefRefPtr<CefRequest> request,
                                    CefRefPtr<CefResponse> response,
                                    CefString& new_url);

    virtual bool OnResourceResponse(CefRefPtr<CefBrowser> browser,
                                    CefRefPtr<CefFrame> frame,
                                    CefRefPtr<CefRequest> request,
                                    CefRefPtr<CefResponse> response);

    virtual CefRefPtr<CefResponseFilter> GetResourceResponseFilter(CefRefPtr<CefBrowser> browser,
                                                                   CefRefPtr<CefFrame> frame,
                                                                   CefRefPtr<CefRequest> request,
                                                                   CefRefPtr<CefResponse> response);

    virtual void OnResourceLoadComplete(CefRefPtr<CefBrowser> browser,
                                        CefRefPtr<CefFrame> frame,
                                        CefRefPtr<CefRequest> request,
                                        CefRefPtr<CefResponse> response,
                                        CefResourceRequestHandler::URLRequestStatus status,
                                        int64 received_content_length);

    virtual void OnProtocolExecution(CefRefPtr<CefBrowser> browser,
                                     CefRefPtr<CefFrame> frame,
                                     CefRefPtr<CefRequest> request,
                                     bool& allow_os_execution);


    //////////////////////////////////////////////////////////////////////////
    //CefDownloadHandler
    virtual void OnBeforeDownload(CefRefPtr<CefBrowser> browser,
                                  CefRefPtr<CefDownloadItem> download_item,
                                  const CefString& suggested_name,
                                  CefRefPtr<CefBeforeDownloadCallback> callback);

    virtual void OnDownloadUpdated(CefRefPtr<CefBrowser> browser,
                                   CefRefPtr<CefDownloadItem> download_item,
                                   CefRefPtr<CefDownloadItemCallback> callback);

private:
    IMPLEMENT_REFCOUNTING(QCefDelegate);
};


#endif // QCEFDELEGATE_H
